package com.linkvin.ec.hcs.service.mdm.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.linkvin.ec.hcs.dao.mapper.mdm.WarehouseLocationMapper;
import com.linkvin.ec.hcs.dao.model.mdm.WarehouseLocation;
import com.linkvin.ec.hcs.dao.model.mdm.bo.WarehouseLocationBo;
import com.linkvin.ec.hcs.service.mdm.WarehouseLocationService;
import com.linkvin.ec.hcs.service.util.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class WarehouseLocationServiceImpl implements WarehouseLocationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WarehouseLocationServiceImpl.class);

    @Autowired
    private WarehouseLocationMapper warehouseLocationMapper;

    @Override
    public Page<WarehouseLocationBo> queryPageWarehouseLoc(Page<WarehouseLocationBo> page) {
        List<WarehouseLocationBo> listDepartBo = warehouseLocationMapper.queryPageWarehouseLoc(page,buildCondition(page.getCondition()));
        page.setRecords(listDepartBo);
        return page;
    }

    @Override
    public WarehouseLocationBo addWarehouseLoc(WarehouseLocationBo warehouseLocationBo) {
        //非空校验
        if (Strings.isNullOrEmpty(warehouseLocationBo.getCode().trim())) throw new BaseException("库位编码不能为空");
        if (Strings.isNullOrEmpty(warehouseLocationBo.getName().trim())) throw new BaseException("库位名称不能为空");
        if (Objects.equals(warehouseLocationBo.getwId(),null)) throw new BaseException("所属仓库不能为空");

        //判断是否重复
        EntityWrapper<WarehouseLocation> ew = new EntityWrapper<>();
        ew.where("w_id={0}",warehouseLocationBo.getwId()).andNew("code={0}", warehouseLocationBo.getCode().trim()).or("name={0}", warehouseLocationBo.getName().trim());


        int count = warehouseLocationMapper.selectCount(ew);
        if(count > 0){
            LOGGER.error("仓库编码或者名称重复");
            throw new BaseException("仓库编码或者名称重复");
        }

        WarehouseLocation  warehouseLocation = new WarehouseLocation();
        BeanUtils.copyProperties(warehouseLocationBo, warehouseLocation);
        count = warehouseLocationMapper.insert(warehouseLocation);

        if(count > 0){
            warehouseLocationBo = getWarehouseLocBo(warehouseLocationBo.getCode());
        }

        return warehouseLocationBo;

    }

    @Override
    public WarehouseLocationBo updateWarehouseLoc(WarehouseLocationBo warehouseLocationBo) {
        //非空校验
        if (Strings.isNullOrEmpty(warehouseLocationBo.getCode().trim())) throw new BaseException("库位编码不能为空");
        if (Strings.isNullOrEmpty(warehouseLocationBo.getName().trim())) throw new BaseException("库位名称不能为空");
        if (Objects.equals(warehouseLocationBo.getwId(),null)) throw new BaseException("所属仓库不能为空");

        //判断是否重复
        EntityWrapper<WarehouseLocation> ew = new EntityWrapper<>();
        ew.where("id != {0}", warehouseLocationBo.getId()).and("w_id={0}",warehouseLocationBo.getwId()).andNew("code={0}", warehouseLocationBo.getCode().trim()).or("name={0}", warehouseLocationBo.getName().trim());
        int count = warehouseLocationMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("仓库编码或者名称重复");
        }

        WarehouseLocation warehouse = new WarehouseLocation();
        BeanUtils.copyProperties(warehouseLocationBo, warehouse);
        count = warehouseLocationMapper.updateById(warehouse);

        if(count > 0){
            warehouseLocationBo = getWarehouseLocBo(warehouseLocationBo.getCode());
        }

        return warehouseLocationBo;
    }

    @Override
    public int deleteWarehouseLoc(List<Integer> warehouseIds ) {
        //TODO 需要判断是否已经使用
        return warehouseLocationMapper.deleteBatchIds(warehouseIds);
    }

    private WarehouseLocationBo getWarehouseLocBo(String code){
        WarehouseLocationBo warehouseBO = new WarehouseLocationBo();

        List<WarehouseLocationBo> listWarehouseBo= warehouseLocationMapper.selectWarehouseLoc(code);
        if(!CollectionUtils.isEmpty(listWarehouseBo)){
            warehouseBO = listWarehouseBo.get(0);
        }
        return warehouseBO;
    }

    private EntityWrapper<WarehouseLocationBo> buildCondition(Map<String,Object> condition){
        EntityWrapper<WarehouseLocationBo> ew = new EntityWrapper<>();

        if(!Objects.equals(condition,null)) {
            for (Map.Entry<String, Object> entry : condition.entrySet()) {
                if (entry.getKey().equals("wId")) {
                    ew.eq("w_id", entry.getValue().toString());
                }
                if (entry.getKey().equals("name")) {
                    ew.like("name", entry.getValue().toString());
                }
            }
        }
        return ew;
    }
}
